<?php

// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      

//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2023/03/10
//      |_|                                         | TpMeCMS

declare(strict_types=1);

namespace ImiApp\ApiServer\Api\Controller\notice;

use Imi\Db\Db;
use Imi\Server\Http\Route\Annotation\Action;
use Imi\Server\Http\Route\Annotation\Controller;
use Imi\Server\Http\Route\Annotation\Route;
use Psr\Http\Message\ResponseInterface;
use Imi\Aop\Annotation\Inject;
// use Phpben\Imi\Validate\Annotation\Validate;
use Phpben\Imi\Auth\Annotation\Auth;

/**
 * 通知
 * @Auth(name="token",nologin={"no"})
 * @Controller("/notice.notice/")
 */
class Notice extends NoticeBean
{
    protected $noNeedLogin = ['no'];
    protected $noNeedRight = ['*'];

    /**
     * 评论通知
     *
     * @param int $notice_id 通知id
     * @param string $content 内容
     * @param string $images 图集(可不传)
     * @param int $comment_id 评论id(可不传)
     * @return void
     * @Action
     */
    public function add_comment($notice_id, $content = null, $comment_id = null, $images = null)
    {
        $val = Db::name('notice')->where('id', '=', $notice_id)->select()->get();
        if (!$val) {
            return $this->error('该通知不存在');
        }
        $where['notice_id'] = $notice_id;
        $this->VerifyStr($content);
        if ($comment_id) {

            $res = Db::name('notice_comment')->where('id', '=',  $comment_id)->whereEx($where)->select()->get();
            if (!$res) {
                return $this->error('该评论不存在');
            }
            if ($res['comment_id'] > 0) {
                $where['top_id'] = $res['top_id'];
            } else {
                $where['top_id'] = $comment_id;
            }
            $where['comment_uid'] = $res['uid']; //被评论的用户id
            // $where['top_status'] = '2';
        } else {
            // $where['top_status'] = '1';
        }
        $where['comment_id'] = $comment_id;

        $where['content'] = $content;
        $where['images'] = $images;
        $where['uid'] = $this->auth->user()->id;
        $where['time'] = time();
        $where['switch'] = 0; //默认都要后台审核
        $id = Db::name('notice_comment')->insert($where)->getLastInsertId();

        // $this->AddMsg($uid,$status,$public_id,$user_id=null,$title=null);//类型:1=通知点赞,2=通知评论,3=提问点赞,4=提问评论,5=活动点赞,6=活动留言,7=聊天消息
        if ($comment_id) {
            if ($res['uid'] != $this->auth->user()->id) //自己评论自己不添加
                $this->AddMsg($res['uid'], 2, $notice_id, $this->auth->user()->id, $this->auth->user()->nickname . '给你通知评论了');
        } else {
            // if ($val['uid'] != $this->auth->user()->id) //自己评论自己不添加
            // $this->AddMsg($val['uid'], 2, $notice_id, $this->auth->user()->id, $this->auth->user()->nickname . '给你通知评论了');
        }



        return $this->success('评论成功', $id);
    }

    /**
     * 获取通知列表
     * @param string $search 搜索内容
     * @param integer $page 1
     * @param integer $limit 10
     * @return void
     * @Action
     */
    public function get_list($search = null, ?int $page = 1, ?int $limit = 3)
    {
        $page = $page ?: 1;
        $limit = $limit ?: 3;
        if ($this->isStore()) {
            $where['notice_status'] = '1'; //公告类型:1=店铺,2=用户
        } else {
            $where['notice_status'] = '2'; //公告类型:1=店铺,2=用户
        }
        $where['status'] = '1'; //公告状态:0=关闭,1=正常
        if ($search) {
            // $where['name'] = ['like', "%" . $search . "%"];
            $list = Db::name('notice')
                ->whereEx($where)
                ->orderRaw('createtime desc')
                ->whereEx(['name'=> ['like', "%" . $search . "%"],'or'=>['content'=>[ 'like', "%" . $search . "%"]]])
                // ->Where('content', 'like', "%" . $search . "%")
                ->page($page, $limit)
                ->select()->getArray();
        } else {
            $list = Db::name('notice')
                ->whereEx($where)
                ->orderRaw('createtime desc')
                ->page($page, $limit)
                ->select()->getArray();
        }


        if ($list)
            foreach ($list as $key => &$val) {
                // var_dump($val);
                $val = $this->ChangeFieldData($val);
            }
        return $this->success($page, $list);
    }

    /**
     * 获取通知详情
     *
     * @param int $id 通知id
     * @return void
     * @Action
     */
    public function get_data($id = null)
    {
        // if ($this->isStore()) {
        //     $where['notice_status'] = '1'; //公告类型:1=店铺,2=用户
        // } else {
        //     $where['notice_status'] = '2'; //公告类型:1=店铺,2=用户
        // }
        $where['status'] = '1'; //公告状态:0=关闭,1=正常
        $where['id'] = $id;
        $notice = Db::name('notice')
            ->whereEx($where)
            ->select()->get();
        if (!$notice) {
            return $this->error('没有该通知');
        }

        $this->AddViews($id);
        $this->ChangeFieldData($notice);

        return $this->success($id, $notice);
    }



    // /**
    //  * 修改点赞
    //  *
    //  * @param int $notice_id 通知id
    //  * @return void
    //  */
    // public function update_likes($notice_id)
    // {
    //     $data['uid'] = $this->auth->user()->id;
    //     $data['notice_id'] = $notice_id;
    //     $zone = Db::name('notice')->where('id',$notice_id)->select()->get();
    //     $res = Db::name('zonelikes')->whereEx($data)->select()->get();
    //     if(!$res){
    //         $data['time'] = time();
    //         if(!$zone)return $this->error('该通知不存在');
    //         $data['zone_uid'] = $zone['uid'];
    //         $data['id'] = Db::name('zonelikes')->insert($data)->getLastInsertId();
    //         //点赞数+1
    //         $this->IncZoneLikes($notice_id);

    //         // $this->AddMsg($uid,$status,$public_id,$user_id=null,$title=null);//类型:1=通知点赞,2=通知评论,3=提问点赞,4=提问评论,5=活动点赞,6=活动留言,7=聊天消息
    //         $this->AddMsg($zone['uid'],1,$notice_id,$this->auth->user()->id,$this->auth->user()->nickname.'给你通知点赞了');

    //         return $this->success('点赞成功',1);
    //     }else{
    //         Db::name('zonelikes')->where('id',$res['id'])->delete();
    //         //点赞数-1
    //         $this->DecZoneLikes($notice_id);

    //         // $this->AddMsg($zone['uid'],1,$notice_id,$this->auth->user()->id,$this->auth->user()->nickname.'给你通知取消点赞了');


    //         return $this->success('取消点赞',0);
    //     }
    // }
}
